﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using BE;
using System.Data;
using CrystalDecisions.Shared;
//using System.Data;

using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;



namespace UI.DireccionAcademica
{
    public partial class FrmReporteCantAlumnosxEspxCiclo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                llenarComboSemestre();
                comboSemestre_SelectedIndexChanged(null, null);
                //Generar();
            }
            {


            }
        }

        private void Generar()
        {
            LabelError.Text = "";
            try
            {               
                String semestre;

                if (comboSemestre.SelectedItem.Text == "--Todos--")
                {
                    semestre = "";
                }
                else
                {
                    semestre = comboSemestre.SelectedItem.Text;
                }

                Alumno_BL obj = new Alumno_BL();
                DataTable dato = new DataTable();
                dato = obj.DATA_MATRIC1_ListaAlumnosEspecialidadvsCiclos_xSemestre(semestre); 
                grvAlumnoxEspxCiclo.DataSource = dato;
                grvAlumnoxEspxCiclo.DataBind();

                CuentaItemsGrilla(dato);             

            }
            catch (Exception ex)
            {
                LabelError.Text = ex.Message.ToString();
            }

        }

        public void CuentaItemsGrilla(DataTable dato)
        {
            if (dato.Rows.Count == 0)
            {
                grvAlumnoxEspxCiclo.Caption = "No se encontró coincidencias";
                //lblMensaje.Text = "No se encontró coincidencias";
            }
            else if (dato.Rows.Count == 1)
            {
                grvAlumnoxEspxCiclo.Caption = "Total de items - 1 registro encontrado";
                //lblMensaje.Text = "";
            }
            else
            {
                grvAlumnoxEspxCiclo.Caption = "Total de items - " + dato.Rows.Count + " registros encontrados";
                //lblMensaje.Text = "";
            }
        }

        private void llenarComboSemestre()
        {
            LabelError.Text = "";
            try
            {
                ReporteNominaComplementaria_BL rnc = new ReporteNominaComplementaria_BL();
                List<Periodo> lista = rnc.ObtenerPeriodos();
                comboSemestre.Items.Add("---Todos---");
                comboSemestre.DataSource = lista;
                comboSemestre.DataTextField = "Semestre";
                comboSemestre.DataValueField = "ID";
                comboSemestre.DataBind();
                comboSemestre.Items.Insert(0, "--Todos--");
            }
            catch (Exception ex)
            {
                LabelError.Text = ex.Message;
            }

        }

        protected void comboSemestre_SelectedIndexChanged(object sender, EventArgs e)
        {
            Generar();
        }

        protected void btnExportar_Click(object sender, EventArgs e)
        {
            ////export to excel
            //Response.Clear();
            //Response.Buffer = true;
            //Response.ContentType = "application/vnd.ms-excel";
            //Response.Charset = "";
            //this.EnableViewState = false;
            //System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            //System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            //this.ClearControls(grvAlumnoxEspxCiclo);
            //GridView bt = new GridView();
            //bt.Attributes.Add("RUNAT", "SERVER");


            //bt.RenderControl(oHtmlTextWriter);
            //Response.Write(oStringWriter.ToString());
            //Response.End();


            //if (GenerarReporte())
            //{
            //    string script = "AbrirVentana('frmReporteActaPromocionalPdf3.aspx')";
            //    ScriptManager.RegisterStartupScript(this, typeof(Page), "popup", script, true);
            //};

            ExportToExcel(null,null);
        }

        protected void ExportToExcel(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                grvAlumnoxEspxCiclo.AllowPaging = false;
                
                //this.BindGrid();
                Generar();

                grvAlumnoxEspxCiclo.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in grvAlumnoxEspxCiclo.HeaderRow.Cells)
                {
                    cell.BackColor = grvAlumnoxEspxCiclo.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in grvAlumnoxEspxCiclo.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = grvAlumnoxEspxCiclo.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = grvAlumnoxEspxCiclo.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                grvAlumnoxEspxCiclo.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }

        //private bool GenerarReporte()
        //{
        //    LabelError.Text = "";
        //    try
        //    {

        //        excel reporte = new excel();

        //        String semestre;

        //        if (comboSemestre.SelectedItem.Text == "--Todos--")
        //        {
        //            semestre = "";
        //        }
        //        else
        //        {
        //            semestre = comboSemestre.SelectedItem.Text;
        //        }

        //        Alumno_BL obj = new Alumno_BL();
        //        DataTable dato = new DataTable();
        //        dato = obj.DATA_MATRIC1_ListaAlumnosEspecialidadvsCiclos_xSemestre(semestre); 

        //        if (dato.Rows.Count == 0)       
        //        {
        //            LabelError.Text = "No se encontraron registros";
        //            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + "No se encontraron registros :" + "" + "');", true);
        //            return false;
        //        }
        //        ExportOptions CrExportOptions;
        //        DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
        //        PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
        //        String rutaarchivo = System.IO.Path.GetTempFileName() + Guid.NewGuid() + ".pdf";
        //        CrDiskFileDestinationOptions.DiskFileName = rutaarchivo;

        //        Session["RutaReporte"] = rutaarchivo;


        //        CrExportOptions = reporte.ExportOptions;
        //        {
        //            CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
        //            CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
        //            CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;

        //            CrExportOptions.FormatOptions = CrFormatTypeOptions;

        //        }

        //        reporte.Export();
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        LabelError.Text = ex.Message;
        //        return false;
        //    }
        //}
        //private void ClearControls(Control control)
        //{
        //    for (int i = control.Controls.Count - 1; i >= 0; i--)
        //    {
        //        ClearControls(control.Controls[i]);
        //    }
        //    if (!(control is TableCell))
        //    {
        //        if (control.GetType().GetProperty("SelectedItem") != null)
        //        {
        //            LiteralControl literal = new LiteralControl();
        //            control.Parent.Controls.Add(literal);
        //            try
        //            {
        //                literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
        //            }
        //            catch
        //            {
        //            }
        //            control.Parent.Controls.Remove(control);
        //        }
        //        else
        //            if (control.GetType().GetProperty("Text") != null)
        //            {
        //                LiteralControl literal = new LiteralControl();
        //                control.Parent.Controls.Add(literal);
        //                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
        //                control.Parent.Controls.Remove(control);
        //            }
        //    }
          
        //    return;
        //}


    }
}